<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Model;

class UserInvite extends Model
{
    protected $table = 'user_invite';
    public $timestamps = false;
    

    public function addInvite($user_id,$code)
    {
        $inv_user = User::where('code', $code)->first();
        if(!$inv_user){
            return -1;
        }

        if($inv_user->id == $user_id){
            return -2;
        }

        $invite = UserInvite::where('user_id', $user_id)->first();
        if($invite){
            return -3;
        }

        $data = new UserInvite();
        $data->user_id = $user_id;
        $data->up_user_id = $inv_user->id;
        $data->create_time = date('Y-m-d H:i:s');
        $data->update_time = date('Y-m-d H:i:s');
        $data->rebate_status = 1;
        $data->rebate = 0;
        $data->save();
        return 1;
    }
}
